/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.certificado.vista;

import basedatos.OperacionesBD;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.Persona;
import modelo.Pyme;
import modulos.agenda.vista.iFrmAgenda;
import modulos.certificado.modelo.Certificado;
import modulos.paciente.modelo.Paciente;
import modulos.usuario.modelo.Usuario;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
import vista.frmPrincipal;

/**
 *
 * @author medikux
 */
public class iFrmCMNuevo extends javax.swing.JInternalFrame {

    private Usuario usuario = new Usuario();
    private Paciente paciente = new Paciente();
    /**
     * Creates new form iFrmCMNuevo
     */
    public iFrmCMNuevo(Paciente paciente) {
        initComponents();
        usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        this.paciente=paciente;
    }

    private Certificado guardarCertificado(){
        Certificado certificado=new Certificado();
        certificado.setContenido(txtContenido.getText());
        certificado.setFecha(new Date());
        certificado.setPaciente(paciente);
        certificado.setUsuario(usuario);
        OperacionesBD.guardar(certificado);
        return certificado;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtContenido = new javax.swing.JTextArea();
        btnImprimir = new javax.swing.JButton();
        bntGuardar = new javax.swing.JButton();

        setClosable(true);
        setTitle("CERTIFICADO MEDICO");

        txtContenido.setColumns(20);
        txtContenido.setRows(5);
        jScrollPane1.setViewportView(txtContenido);

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/printer1.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });

        bntGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        bntGuardar.setText("Guardar");
        bntGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 787, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bntGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(64, 64, 64)
                        .addComponent(btnImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(257, 257, 257))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnImprimir)
                    .addComponent(bntGuardar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-822)/2, (screenSize.height-526)/2, 822, 526);
    }// </editor-fold>//GEN-END:initComponents

    private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
         //Cargas Datos de Compañia
        ArrayList<Pyme> list = (ArrayList<Pyme>) OperacionesBD.listar("Pyme");
        Pyme pyme = null;
        if (!list.isEmpty()) {
            pyme = list.get(0);
        } else {
            pyme = new Pyme();
        }
        Certificado certificado= guardarCertificado();
        DateFormat d= DateFormat.getDateInstance(DateFormat.LONG);
        String fecha=d.format(certificado.getFecha());
        String medico="";
        if(usuario.getGenero().equalsIgnoreCase("Femenino")){
            medico="Dra. " + usuario.getMedico();
        }else{
            medico="Dr. " + usuario.getMedico();
        }
        String pacient="";
        if(paciente.getGenero().equalsIgnoreCase("Masculino")){
            pacient="Que el paciente Sr. "+paciente;
        }else{
            pacient="Que la paciente Sra. "+ paciente;
        }
        Map<String, Object> parametros = new HashMap<String, Object>();
        parametros.put("COMPANIA", pyme.getNombre());
        parametros.put("FECHA", fecha);
        parametros.put("CONTENIDO", certificado.getContenido());
        parametros.put("MEDICO", medico);
        parametros.put("PACIENTE", pacient);
        parametros.put("CEDULA", paciente.getCedula());
       parametros.put("ESPECIALIDAD",usuario.getEspecialidad());
       
        ArrayList aux = new ArrayList();
        aux.add("aux");

        try {
            JasperReport reporte = (JasperReport) JRLoader.loadObject(getClass().getResource("/modulos/reportes/certificado/Certificado.jasper"));

            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JRBeanCollectionDataSource(aux));
            JasperViewer vista = new JasperViewer(jasperPrint, false);
            vista.setSize(frmPrincipal.dskpPrincipal.size());
            vista.setVisible(true);
        } catch (JRException ex) {
            Logger.getLogger(iFrmAgenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        iFrmCertificadoMedico.cargarCertificados();
        dispose();
    }//GEN-LAST:event_btnImprimirActionPerformed

    private void bntGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntGuardarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bntGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntGuardar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtContenido;
    // End of variables declaration//GEN-END:variables
}
